﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using Celeriq.Common;
using PJB.FacetedNavigation;

namespace PJBWebsite.UserControls
{
    public partial class ResultsFilterDisplayControl : System.Web.UI.UserControl
    {

        #region Page Events

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        #endregion

        #region Methods

        public void Populate(ListingResults results, ListingQuery query)
        {
            var masterResults = RepositoryHelper.MasterResults;

            //Group items by dimension
            var dimList = new Dictionary<string, List<RefinementItem>>();
            foreach (var dvidx in results.Query.DimensionValueList)
            {
                foreach (var dItem in masterResults.DimensionList)
                {
                    var rItem = dItem.RefinementList.FirstOrDefault(x => x.DVIdx == dvidx);
                    if (rItem != null)
                    {
                        if (!dimList.ContainsKey(dItem.Name)) dimList.Add(dItem.Name, new List<RefinementItem>());
                        dimList[dItem.Name].Add(rItem);
                    }
                }
            }

            var count = 0;

            //Loop through dimensions and create a list of filters
            foreach (var dName in dimList.Keys)
            {
                var dText = dName;

                //Create the label
                var l = new Literal();
                l.Text = "<div><span class=\"prompt\">" + dText + "</span>: ";

                var rCount = 0;
                foreach (var rItem in dimList[dName])
                {
                    //Create the Dimension Value
                    var fieldValue = (rCount > 0 ? ", &nbsp;" : "") + rItem.FieldValue;
                    l.Text += fieldValue;
                    l.Text += this.CreateRemoveLink(this.Request.Url.AbsoluteUri, rItem.DVIdx);
                    count++;
                    rCount++;
                }
                l.Text += "</div>";
                pnlFilterDisplay.Controls.Add(l);
            }

            //If there is text filter then add it too
            if (!string.IsNullOrEmpty(results.Query.Keyword))
            {
                //Create the label
                var l = new Literal();
                l.Text = "<div><span class=\"prompt\">Text filter</span>: " + results.Query.Keyword;
                l.Text += this.CreateRemoveLinkNoTextFilter(this.Request.Url.AbsoluteUri);
                l.Text += "</div>";
                pnlFilterDisplay.Controls.Add(l);
                count++;
            }

            var geoFilter = results.Query.FieldFilters.FirstOrDefault(x => x is FieldFilterLocation) as FieldFilterLocation;
            if (geoFilter != null)
            {
                //Create the label
                var l = new Literal();
                var zip = results.Query.NonParsedFieldList.Where(x => x.Key == "zip").Select(x => x.Value).FirstOrDefault();
                var radius = results.Query.NonParsedFieldList.Where(x => x.Key == "rd").Select(x => x.Value).FirstOrDefault();
                l.Text = "<div><span class=\"prompt\">Zip</span>: " + zip + " for " + radius + " miles";
                l.Text += this.CreateRemoveLinkNoTextFilter(this.Request.Url.AbsoluteUri);
                l.Text += "</div>";
                pnlFilterDisplay.Controls.Add(l);
                count++;
            }

            //Do not show this if there is nothing to show
            if (count == 0)
                this.Visible = false;
        }

        private string CreateRemoveLink(string queryString, long dvidx)
        {
            var query = new ListingQuery(queryString);
            query.DimensionValueList.Remove(dvidx);
            return "<a title=\"Remove this filter\" rel=\"nofollow\" href=\"" + query.ToString() + "\"><img src=\"/images/trash.gif\" /></a>";
        }

        private string CreateRemoveLinkNoTextFilter(string queryString)
        {
            var query = new ListingQuery(queryString);
            query.Keyword = string.Empty;
            return "<a title=\"Remove this filter\" rel=\"nofollow\" href=\"" + query.ToString() + "\"><img src=\"/images/trash.gif\" /></a>";
        }

        #endregion

    }
}